package com.woniuxy.bookmall.utils;


import cn.hutool.core.io.FileUtil;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class GeneratorUtil {
    private static String backLocation;
    private static String mapperLocation;

    static {
        Properties pro = new Properties();
        try(InputStream is = GeneratorUtil.class.getClassLoader().getResourceAsStream("db.properties")){
            pro.load(is);
            //portal\\src\\main\\java
            String targetEntityProjectWindow = pro.getProperty("targetEntityProjectWindow");
            //sh.wn78.portal.mapper
            String targetMapperPackage = pro.getProperty("targetMapperPackage");
            targetEntityProjectWindow = targetEntityProjectWindow.replace("\\",".");
            mapperLocation = targetEntityProjectWindow + "." + targetMapperPackage;
            mapperLocation = mapperLocation.replace(".", File.separator);
            System.out.println(mapperLocation);
            backLocation = mapperLocation.replace("mapper","back");
            backLocation = backLocation.replace(".",File.separator);
            System.out.println(backLocation);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        //--1. 备份 当数据表发生变更需要重新逆向生成代码，会把Mapper接口给覆盖掉。如果Mapper接口中已经写了方法，就丢失了。所以需要先备份Mapper接口
        //backups();
        //--2. 执行逆向
        //mbg();
        //--3. 还原 用备份的Mapper接口覆盖重新生成的Mapper接口，删除备份的内容
       // restore();
    }

    private static void restore() {
        //-- back下面有内容时才需要还原
        File backFolder = new File(backLocation);
        //-- 获取mapper下的所有内容
        File[] files = backFolder.listFiles();
        if (!(files == null || files.length == 0)) {
            //-- 代表mapper不是空的
            File mapperFolder = new File(backLocation.replace("back","mapper"));
            FileUtil.copyContent(backFolder,mapperFolder,true);
            //-- back 删除
            FileUtil.del(backFolder);
        }
    }
    private static void backups() {
        //-- 判断mapper下是否有内容，如果有备份
        File mapperFolder = new File(mapperLocation);
        //-- 获取mapper下的所有内容
        File[] files = mapperFolder.listFiles();
        if (!(files == null || files.length == 0)){
            //-- 代表mapper不是空的
            File backUpFolder = new File(mapperLocation.replace("mapper","back"));
            FileUtil.copyContent(mapperFolder,backUpFolder,false);
        }else{
            System.out.println("不备份！");
        }
        //-- 代表mapper是空的 不需要备份
    }
    public static void mbg(){
        try{
            List<String> warnings = new ArrayList<>();
            boolean overwrite = true;
            ConfigurationParser cp = new ConfigurationParser(warnings);
            Configuration config = cp.parseConfiguration(
                    GeneratorUtil.class.getClassLoader().getResourceAsStream("generator.xml"));
            DefaultShellCallback callback = new DefaultShellCallback(overwrite);
            MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
            myBatisGenerator.generate(null);
        } catch (XMLParserException | SQLException | IOException | InterruptedException |
                 InvalidConfigurationException e) {
            throw new RuntimeException(e);
        }
    }
}

